import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/views/Layout.vue'),
    meta: { requiresAuth: true },
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '数据概览', icon: 'DataAnalysis' }
      },
      {
        path: '/store',
        name: 'Store',
        component: () => import('@/views/Store.vue'),
        meta: { title: '店铺管理', icon: 'Shop' }
      },
      {
        path: '/menu',
        name: 'Menu',
        component: () => import('@/views/Menu.vue'),
        meta: { title: '菜品管理', icon: 'Food' }
      },
      {
        path: '/orders',
        name: 'Orders',
        component: () => import('@/views/Orders.vue'),
        meta: { title: '订单管理', icon: 'List' }
      },
      {
        path: '/promotions',
        name: 'Promotions',
        component: () => import('@/views/Promotions.vue'),
        meta: { title: '营销活动', icon: 'Present' }
      },
      {
        path: '/reviews',
        name: 'Reviews',
        component: () => import('@/views/Reviews.vue'),
        meta: { title: '评价管理', icon: 'ChatDotRound' }
      },
      {
        path: '/finance',
        name: 'Finance',
        component: () => import('@/views/Finance.vue'),
        meta: { title: '财务统计', icon: 'Money' }
      },
      {
        path: '/settings',
        name: 'Settings',
        component: () => import('@/views/Settings.vue'),
        meta: { title: '系统设置', icon: 'Setting' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  if (to.meta.requiresAuth !== false && !authStore.isLoggedIn) {
    next('/login')
  } else if (to.path === '/login' && authStore.isLoggedIn) {
    next('/')
  } else {
    next()
  }
})

export default router 